import pandas as pd
import numpy as np
from matplotlib import pyplot as plt
import matplotlib.ticker as ticker

# 设置中文显示
plt.rcParams['font.sans-serif'] = ['SimHei']

# 1. 创建2个列表：岗位名称job_list和平均工资空列表job_salary
job_salary = []
job_list = ["人工智能", "前端", "图像", "安卓", "数据", "算法", "软件", "Android", "Java", "运维"]

# 2. for语句，依次读取各个岗位工资数据，并计算平均值salary_average
for job in job_list:
    data = pd.read_excel('E:\\pythonstudy\\pythonProject1\\data4\\data4_3_classify_job\\{}.xlsx'.format(job))
    data = data.drop('Unnamed: 0', axis=1)
    salary_average = int(data['salary'].mean())
    print("这是{}岗位的平均工资{}".format(job, salary_average))
    job_salary.append(salary_average)

# 3. 将各个岗位平均工资值追加到job_salary中
# print(data)

# 4. 将工资平均值和岗位名称列表拼接在一起
data_new = pd.DataFrame({
    "岗位名称": job_list,
    "平均工资": job_salary
})

# 5. 按照工资对数据进行重新排序
data_new = data_new.sort_values(by='平均工资', ascending=False)
data_new = data_new.reset_index(drop=True)

# 计算平均工资的平均值
average_1 = data_new['平均工资'].mean()
print(data_new)

# 6. 显示折线图
data_new.plot(kind='line', x='岗位名称', y='平均工资', color='red', marker='o', linestyle='-', markersize=5, markeredgecolor='red', markerfacecolor='red')

# 7. 折线图上数字显示
average_salary = data_new['平均工资'].tolist()
job_name = data_new["岗位名称"].tolist()
for x, y in enumerate(average_salary):
    plt.text(x, y, y, ha='center', fontsize=12)

# 8. 折线图上平均线和平均值显示
plt.plot([0, len(job_name) - 1], [average_1, average_1], color='b', linewidth=2)
plt.text(6, average_1 + 100, '平均值:', fontsize=11, color='b')
plt.text(7, average_1 + 100, average_1, fontsize=11, color='b')

# 9. 图标的显示配置和保存
plt.ylim(8000, 18000)
plt.title("各种岗位平均工资排序")
plt.xlabel("岗位名称", fontsize=16)
plt.ylabel("平均工资", fontsize=16)
plt.xticks(range(len(job_name)), job_name, fontsize=10, rotation=0)
plt.tight_layout()
# plt.figure(figsize=(12, 6))
plt.savefig('./picture/5 - 3.各种岗位平均工资排序.png')
plt.show()